package com.surprise.db.dao;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.surprise.db.entity.Mall;
import com.surprise.db.entity.join.SelectMall001Entity;
import com.surprise.db.vo.IdNameVO;

/**
 * MallDAO.java
 * @author nick
 * @since 2015年5月9日
 */
@Repository
public class MallDAO extends SimpleDAO<Mall> {
	public final String columns = "(name,logo,cityid,address,phone,addtime)";
	public final String insertStatement = "(?,?,?,?,?,now())";
	
//	@AllArgsConstructor
//	public static class MallSetter implements BatchPreparedStatementSetter {
//		private List<Mall> items;
//		
//		@Override
//		public void setValues(PreparedStatement ps, int i) throws SQLException {
//			Mall item = items.get(i);
//			int index = 1;
//			ps.setString(index++, item.getName());
////			ps.setString(index++, item.getLogo());
//			ps.setLong(index++, item.getCityid());
//			ps.setString(index++, item.getAddress());
////			ps.setString(index++, item.getPhone());
//		}
//
//		@Override
//		public int getBatchSize() {
//			return items.size();
//		}
//	}
//	
//	public void add(List<Mall> items) {
//		StringBuilder sb = new StringBuilder();
//		sb.append("insert into ")
//			.append(getTableName())
//			.append(columns)
//			.append(" values ")
//			.append(insertStatement);
//		String sql = sb.toString();
//		jdbcTemplate.batchUpdate(sql, new MallSetter(items));
//	}
//	
//	public void edit(Mall item) {
//		StringBuilder sb = new StringBuilder();
//		sb.append("update ")
//			.append(getTableName())
//			.append(" set name=?,logo=?,cityid=?,address=?,phone=? where id = ?");
//		String sql = sb.toString();
//		jdbcTemplate.update(sql, item.getName(),item.getLogo(),item.getCityid(),item.getAddress(),item.getPhone(),item.getId());
//	}
	
	public List<Mall> getMallsByCity(long cityid) {
		String sql = "select * from #table# where cityid = ?";
		return super.findBySql(sql, cityid);
	}
	
	public List<Mall> findAllById() {
        StringBuilder sb = new StringBuilder();
        sb.append("select * from ")
            .append(getTableName())
            .append(" order by id desc");
        List<Mall> ret = (List<Mall>) jdbcTemplate.query(sb.toString(),
                new BeanPropertyRowMapper<Mall>(Mall.class));
        return ret;
    }
	
	/**
	 * 根据category获取拥有此类别的MallList
	 * @param category
	 * @return
	 */
	public List<Mall> getMallsByCategory(long category) {
		String sql = "select m.id,m.name,m.address from mall m where 1=1 and id in(select s.mallid from shop s left join  brand b on s.id = b.shopid  where b.category = ? group by s.mallid)";
		return super.findBySql(sql, category);
	}
	
	/**
	 * 查询签约的shop的MallList集合
	 * @param category
	 * @return
	 */
	public List<Mall> getShopMalls() {
		String sql = "select id,name,address from mall where id in(select mallid from shop group by mallid)";
		return super.findBySql(sql);
	}
	
	/**
	 * 根据城市id获取区域id
	 * @param cityId
	 * @return
	 */
	public List<IdNameVO> findMallListBydistrictId(Long districtId){
		String sql ="select id,name from mall where 1=1 and districtid ="+districtId;
		return jdbcTemplate.query(sql, new Object[] {  }, new BeanPropertyRowMapper<IdNameVO>(IdNameVO.class));
	};
	//查询有品牌商家或者觅食商家的商场
	public List<IdNameVO> findMallListBydistrictId(Long districtId,String type){
		String sql ="select id,name from mall where districtid ="+districtId;
		//FOOD
		if ("10".equals(type)) {
			sql=sql+" and id in (select mallid from food GROUP BY mallid)";
		}
		//brand
		if ("20".equals(type)) {
			sql=sql+" and id in (select mallid from brand GROUP BY mallid )";
		}
		//experience
		if ("30".equals(type)) {
			sql=sql+" and id in (select mallid from experience GROUP BY mallid )";
		}
		return jdbcTemplate.query(sql, new Object[] {  }, new BeanPropertyRowMapper<IdNameVO>(IdNameVO.class));
	};
	
	
	//根据商场区域ID，查询区域名称
	public List<IdNameVO> findDistrictNameByMallid(Long id){
		String sql ="select id,name from city where id ="+id;
		return jdbcTemplate.query(sql, new Object[] {  }, new BeanPropertyRowMapper<IdNameVO>(IdNameVO.class));
	};
}
